package guestbook;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Update extends JInternalFrame {
	private JLabel lblNewLabel;
	private JTextField serial;
	private JLabel lblNewLabel_1;
	private JTextField mid;
	private JLabel lblNewLabel_2;
	private JTextField subject;
	private JScrollPane qw;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JLabel label;
	private JTextField textField_3;

	Connection con;
	GuestBookMain gbm;
	private JTextPane content;
	private JButton btnNewButton_2;

	/**
	 * Create the frame.
	 */
	public Update(Connection c, GuestBookMain o) {
		super("update", true, true, true, true);
		this.con = c;
		this.gbm = o;

		setTitle("방명록 입력및 삭제");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getSerial());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getMid());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getSubject());
		getContentPane().add(getQw());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getLabel());
		getContentPane().add(getTextField_3());
		getContentPane().add(getBtnNewButton_2());

	}

	public void select(int s) {
		try {
			String sql = "select * from guestbook where serial =?";
			PreparedStatement pstmt = gbm.con.prepareStatement(sql);
			pstmt.setInt(1, s);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				serial.setText("" + rs.getInt("serial"));
				mid.setText(rs.getString("mid"));
				subject.setText(rs.getString("subject"));
				content.setText(rs.getString("content"));

			} else {
				JOptionPane.showMessageDialog(null, "찾는 데이터가 없습니다.");
				mid.setText("");
				subject.setText("");
				content.setText("");
			}
			rs.close();
			pstmt.close();

		} catch (Exception ex) {

		}
	}

	public void update() {
		String sql = "update guestbook set mid=?, subject=?, content=? where serial=?";
		try {
			PreparedStatement pstmt = gbm.con.prepareStatement(sql);
			pstmt.setString(1, mid.getText());
			pstmt.setString(2, subject.getText());
			pstmt.setString(3, content.getText());
			pstmt.setInt(4, Integer.parseInt(serial.getText()));

			pstmt.executeUpdate();

			if (gbm.select != null) { // 행의 내용을 실시간으로 바꿔
				gbm.select.setData();
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	public void delete() {
		String sql = "delete from guestbook where serial = ?";
		try {
			PreparedStatement pstmt = gbm.con.prepareStatement(sql);
			pstmt.setInt(1, Integer.parseInt(serial.getText()));

			pstmt.executeUpdate();

			if (gbm.select != null) {
				gbm.select.setData();
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("NO :");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}

	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setBounds(81, 7, 116, 21);
			serial.setColumns(10);
		}
		return serial;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("닉네임 :");
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_1.setBounds(12, 42, 57, 15);
		}
		return lblNewLabel_1;
	}

	private JTextField getMid() {
		if (mid == null) {
			mid = new JTextField();
			mid.setBounds(81, 38, 72, 21);
			mid.setColumns(10);
		}
		return mid;
	}

	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("제목 :");
			lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_2.setBounds(12, 72, 57, 15);
		}
		return lblNewLabel_2;
	}

	private JTextField getSubject() {
		if (subject == null) {
			subject = new JTextField();
			subject.setBounds(81, 69, 341, 21);
			subject.setColumns(10);
		}
		return subject;
	}

	private JScrollPane getQw() {
		if (qw == null) {
			qw = new JScrollPane();
			qw.setBounds(12, 97, 410, 164);
			qw.setViewportView(getTextPane_1());
		}
		return qw;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("수정");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					update();
					// int s = Integer.parseInt(serial.getText());
					// String sql = "select * from guestbook where serial = ?";
					//
					// try {
					// PreparedStatement pstmt = con.prepareStatement(sql);
					// pstmt.setInt(1, s);
					//
					// ResultSet rs = pstmt.executeQuery();
					// if (rs.next()) {
					// mid.setText(rs.getString("mid"));
					// subject.setText(rs.getString("subject"));
					// content.setText(rs.getString("content"));
					//
					// }
					//
					// rs.close();
					// pstmt.close();
					// } catch (Exception ex) {
					// ex.printStackTrace();

				}
			});
			btnNewButton.setBounds(231, 6, 97, 23);
		}
		return btnNewButton;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("삭제");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					delete();
					// String find = subject.getText();
					//
					// String sql =
					// "delete guestbook set mid = ? , subJect = ? , content= ? where serial=? ";
					// try {
					// PreparedStatement pstmt = con.prepareStatement(sql);
					// pstmt.setString(1, find);
					// pstmt.setString(2, "%" + find + "%");
					// pstmt.setString(3, "%" + find + "%");
					// pstmt.executeUpdate();
					//
					// pstmt.close();
					// } catch (Exception ex) {
					// ex.printStackTrace();
				}

			});
			btnNewButton_1.setBounds(325, 38, 97, 23);
		}
		return btnNewButton_1;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("비밀번호 :");
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setBounds(165, 42, 57, 15);
		}
		return label;
	}

	private JTextField getTextField_3() {
		if (textField_3 == null) {
			textField_3 = new JTextField();
			textField_3.setColumns(10);
			textField_3.setBounds(234, 38, 72, 21);
		}
		return textField_3;
	}

	private JTextPane getTextPane_1() {
		if (content == null) {
			content = new JTextPane();
		}
		return content;
	}

	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("검색");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s = Integer.parseInt(serial.getText());
					select(s);

				}
			});
			btnNewButton_2.setBounds(340, 5, 81, 24);
		}
		return btnNewButton_2;
	}
}
